﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Web.Http;
using Benecura.Data.Model;
using Benecura.Data.Repository;

namespace Benecura.Services.WebAPI.Controllers
{
    public class EventController : ApiController
    {
        //api/event
        public List<Event> Get()
        {
            using (EventRepository repo = new EventRepository())
            {
                return repo.GetMoreEvents(e => e.Id < 100);
            }
        }

        //api/event/?count=05-May-2012
        public int GetEventCount(string count)
        {
            DateTime d = DateTime.MaxValue;
            if (DateTime.TryParseExact(count, "dd-MMM-yyyy", null, System.Globalization.DateTimeStyles.None, out d))
            {
                using (EventRepository repo = new EventRepository())
                {
                    return repo.GetMoreEvents(e => ((DateTime)EntityFunctions.TruncateTime(e.ScheduleDateTimeFrom)).Equals(d)).Count;
                }
            }
            else
            {
                return 0;
            }
        }

        //data/event/?action=count&date=05-May-2012
        /*public object Get(string operation, string param)
        {
            switch (operation.ToLower())
            {
                case "count":
                    DateTime d = DateTime.MaxValue;
                    if (DateTime.TryParseExact(param, "dd-MMM-yyyy", null, System.Globalization.DateTimeStyles.None, out d))
                    {
                        EventRepository repo = new EventRepository();
                        var obj = new
                        {
                            Count = repo.GetMore(e => ((DateTime)EntityFunctions.TruncateTime(e.ScheduleDateTimeFrom)).Equals(d)).Count
                        };
                        return obj;
                    }
                    else
                    {
                        return new { Count = 0 };
                    }
                default:
                    return null;
            }
        }*/

        //api/event/5
        public Event Get(int id)
        {
            using (EventRepository repo = new EventRepository())
            {
                return repo.GetEvent(id);
            }
        }

        // POST /api/values
        public void Post(string value)
        {
        }

        // PUT /api/values/5
        public void Put(int id, string value)
        {
        }

        // DELETE /api/values/5
        public void Delete(int id)
        {
        }
    }
}